package com.psmz.mqim.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.psmz.mqim.settings.MQSeriesSettings;
import javax.swing.SwingConstants;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class InitDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7213063060515334225L;
	private final JPanel contentPanel = new JPanel();
	private JTextField hostField;
	private JTextField portField;
	private JTextField sendQueueField;
	private JTextField receiveQueueField;
	private JTextField channelField;
	private JTextField managerField;
	private MQSeriesSettings settings;
	private JTextField nameField;
	private String userName;

	public InitDialog(MQSeriesSettings settings) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(1);
			}
		});
		this.settings = settings;
		createDialog();
	}

	/**
	 * Create the dialog.
	 */
	private void createDialog() {
		setBounds(100, 100, 516, 343);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		setModal(true);
		{
			hostField = new JTextField();
			hostField.setText("localhost");
			hostField.setBounds(79, 87, 86, 20);
			contentPanel.add(hostField);
			hostField.setColumns(10);
		}
		{
			portField = new JTextField();
			portField.setText("1414");
			portField.setBounds(79, 118, 86, 20);
			contentPanel.add(portField);
			portField.setColumns(10);
			portField.setInputVerifier(new InputVerifier() {

				@Override
				public boolean verify(JComponent input) {
					JTextField field = (JTextField) input;
					try {
						Integer.parseInt(field.getText());
					} catch (NumberFormatException e) {
						return false;
					}
					return true;

				}
			});
		}
		{
			sendQueueField = new JTextField();
			sendQueueField.setText("instant_messages_queue");
			sendQueueField.setBounds(203, 143, 86, 20);
			contentPanel.add(sendQueueField);
			sendQueueField.setColumns(10);
		}
		{
			JLabel sendQueueLabel = new JLabel(
					"Kolejka do wysylania komunikatow");
			sendQueueLabel.setBounds(22, 146, 178, 14);
			contentPanel.add(sendQueueLabel);
		}
		{
			JLabel receiveQueueLabel = new JLabel(
					"Kolejka do odbierania komunikatow");
			receiveQueueLabel.setBounds(23, 171, 167, 14);
			contentPanel.add(receiveQueueLabel);
		}
		{
			JLabel hostLabel = new JLabel("Host");
			hostLabel.setBounds(23, 90, 46, 14);
			contentPanel.add(hostLabel);
		}
		{
			JLabel portLabel = new JLabel("Port");
			portLabel.setBounds(23, 115, 46, 14);
			contentPanel.add(portLabel);
		}
		{
			JLabel channelLabel = new JLabel("Kanal");
			channelLabel.setBounds(22, 196, 46, 14);
			contentPanel.add(channelLabel);
		}
		{
			JLabel managerLabel = new JLabel("Manager");
			managerLabel.setBounds(22, 224, 46, 14);
			contentPanel.add(managerLabel);
		}

		receiveQueueField = new JTextField();
		receiveQueueField.setText("instant_messages_queue");
		receiveQueueField.setHorizontalAlignment(SwingConstants.LEFT);
		receiveQueueField.setBounds(203, 174, 86, 20);
		contentPanel.add(receiveQueueField);
		receiveQueueField.setColumns(10);

		channelField = new JTextField();
		channelField.setText("instant_msg_channel");
		channelField.setBounds(79, 196, 86, 20);
		contentPanel.add(channelField);
		channelField.setColumns(10);

		managerField = new JTextField();
		managerField.setText("instant_manager");
		managerField.setBounds(79, 221, 86, 20);
		contentPanel.add(managerField);
		managerField.setColumns(10);
		{
			nameField = new JTextField();
			nameField.setBounds(79, 49, 86, 20);
			contentPanel.add(nameField);
			nameField.setColumns(10);
		}
		{
			JLabel userNameLabel = new JLabel("Imie");
			userNameLabel.setBounds(22, 52, 46, 14);
			contentPanel.add(userNameLabel);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String channelName = channelField.getText();
						String hostName = hostField.getText();
						String portName = portField.getText();
						String sendQueueName = sendQueueField.getText();
						String receiveQueueName = receiveQueueField.getText();
						String managerName = managerField.getText();
						String currentUserName = nameField.getText();
						if (channelName == null || channelName.isEmpty()
								|| hostName == null || hostName.isEmpty()
								|| portName == null || portName.isEmpty()
								|| sendQueueName == null
								|| sendQueueName.isEmpty()
								|| receiveQueueName == null
								|| receiveQueueName.isEmpty()
								|| managerName == null || managerName.isEmpty()
								|| currentUserName == null
								|| currentUserName.isEmpty()) {
							JOptionPane.showMessageDialog(InitDialog.this,
									"Wszystkie wartosci sa wymagane!", "Blad!",
									JOptionPane.ERROR_MESSAGE);
						} else {
							settings.setChannelName(channelName.trim());
							settings.setHostname(hostName.trim());
							settings.setSendQueueName(sendQueueName.trim());
							settings.setReceiveQueueName(receiveQueueName
									.trim());
							settings.setQueueManager(managerName.trim());
							userName = currentUserName.trim();
							try {
								settings.setPort(Integer.valueOf(portName));
								setVisible(false);
							} catch (NumberFormatException ex) {
								JOptionPane.showMessageDialog(InitDialog.this,
										"Niepoprawny format portu!", "Blad!",
										JOptionPane.ERROR_MESSAGE);
							}
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						System.exit(1);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public String getUserName() {
		return userName;
	}

}
